package com.example.uiautomator;

import android.app.Instrumentation;
import android.view.KeyEvent;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class MKTest2 {
    public Instrumentation mInstrumentation;
    public UiDevice mUiDevice;

    @Before
    public void setUp() throws InterruptedException {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mUiDevice = UiDevice.getInstance(mInstrumentation);

        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_HOME);
        Thread.sleep(2000);
    }

    @Test
    public void standardAddTest() throws InterruptedException {

        //3. 打开计算器  计算器图标  坐标点击
        mUiDevice.click(137, 1069);
        Thread.sleep(2000);

        //5. 点击9  9按钮点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit9")).click();
        //6. 按+  + 按键
        mUiDevice.findObject(By.res("com.android.calculator2:id/plus")).click();
        //7. 按键5 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit5")).click();
        //8. 点击= 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/equal")).click();


        //9. 断言验证结果，获取值断言
        UiObject2 ac = mUiDevice.findObject(By.res("com.android.calculator2:id/display")).getChildren().get(0);
        String sum = ac.getText();
        Assert.assertEquals("14", sum);

    }

    @Test
    public void standardSubTest() throws InterruptedException {

        //3. 打开计算器  计算器图标  坐标点击
        mUiDevice.click(137, 1069);
        Thread.sleep(2000);

        //5. 点击9  9按钮点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit9")).click();
        //6. 按-  - 按键
        mUiDevice.findObject(By.res("com.android.calculator2:id/minus")).click();
        //7. 按键5 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit5")).click();
        //8. 点击= 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/equal")).click();


        //9. 断言验证结果，获取值断言
        UiObject2 ac = mUiDevice.findObject(By.res("com.android.calculator2:id/display")).getChildren().get(0);
        String sum = ac.getText();
        Assert.assertEquals("4", sum);

    }

    @Test
    public void standardMulTest() throws InterruptedException {

        //3. 打开计算器  计算器图标  坐标点击
        mUiDevice.click(137, 1069);
        Thread.sleep(2000);

        //5. 点击9  9按钮点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit9")).click();
        //6. 按x  x 按键
        mUiDevice.findObject(By.res("com.android.calculator2:id/mul")).click();
        //7. 按键5 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit5")).click();
        //8. 点击= 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/equal")).click();


        //9. 断言验证结果，获取值断言
        UiObject2 ac = mUiDevice.findObject(By.res("com.android.calculator2:id/display")).getChildren().get(0);
        String sum = ac.getText();
        Assert.assertEquals("45", sum);

    }
    @Test
    public void standardDivTest() throws InterruptedException {

        //3. 打开计算器  计算器图标  坐标点击
        mUiDevice.click(137, 1069);
        Thread.sleep(2000);

        //5. 点击10  10按钮点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit8")).click();
        //6. 按x  x 按键
        mUiDevice.findObject(By.res("com.android.calculator2:id/div")).click();
        //7. 按键5 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit4")).click();
        //8. 点击= 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/equal")).click();


        //9. 断言验证结果，获取值断言
        UiObject2 ac = mUiDevice.findObject(By.res("com.android.calculator2:id/display")).getChildren().get(0);
        String sum = ac.getText();
        Assert.assertEquals("2", sum);

    }
    @After
    public void tearDown(){
        //恢复场景  删除计算结果 按键长按
        //4. 长按C清除  C按键  长按
        mUiDevice.findObject(By.res("com.android.calculator2:id/clear")).longClick();
        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_BACK);
    }

}
